var fs = require("fs");
var path = require("path"); //解析需要遍历的文件夹

//调用文件遍历方法
var reportArray = [];
function fileDisplay(filePath) {
  //根据文件路径读取文件，返回文件列表
  fs.readdir(filePath, function(err, files) {
    if (err) {
      console.warn(err);
    } else {
      files.forEach(function(filename) {
        if (filename.startsWith("report.") && filename.endsWith(".json")) {
          let tmpPath = path.join(filePath, filename);
          let data = fs.readFileSync(tmpPath);
          let json = JSON.parse(data);
          console.log(
            `${json.entryFilePath}\t${json.dependenciesSize}\t${json.dependenciesWithoutCommonSize}`
          );
          if (json.entryFilePath.includes("android")) {
            reportArray.push(json);
          }
        }
      });

      let data = new Array();
      let importCountMap = new Map();

      for (let index = 0; index < reportArray.length; index++) {
        data[index] = new Array();
        let element = reportArray[index];

        element.dependenciesWithoutCommon.forEach(eleId => {
          let tmpCount = importCountMap.get(eleId);
          tmpCount = tmpCount == null ? 1 : tmpCount + 1;
          importCountMap.set(eleId, tmpCount);
        });

        console.log(`${element.entryFilePath}`);
        for (let j = 0; j < reportArray.length; j++) {
          let tmp = reportArray[j];
          let intersection = element.dependenciesWithoutCommon.filter(v =>
            tmp.dependenciesWithoutCommon.includes(v)
          );
          data[index][j] = intersection.length;
        }
      }

      for (let index = 0; index < data.length; index++) {
        let tmp = "";
        for (let j = 0; j < data[index].length; j++) {
          tmp = tmp + "\t" + data[index][j];
        }
        console.log(tmp);
      }

      for (let index = 0; index < data.length; index++) {
        let tmp = "";
        for (let j = 0; j < data[index].length; j++) {
          let obj = reportArray[j];
          let num = new Number(
            data[index][j] / obj.dependenciesWithoutCommonSize
          );
          tmp = tmp + " " + num.toFixed(3);
        }
        console.log(tmp);
      }

      console.log(`############## print map ##############`);
      let repodata = fs.readFileSync(
        `${process.cwd()}/__async_load_output__/repo_for_ids.json.backup`
      );
      let repoObj = JSON.parse(repodata);
      let repoArray = repoObj.data;
      let findMap = new Map();
      repoArray.forEach(ele => {
        findMap.set(ele.id, ele.relativePath);
      });

      let depStaticsPath = `${process.cwd()}/__async_load_output__/depStatics.txt`;
      fs.writeFileSync(depStaticsPath, ``);
      var arrayObj = Array.from(importCountMap);
      arrayObj.sort(function(a, b) {
        return b[1] - a[1];
      });
      fs.appendFileSync(
        depStaticsPath,
        `模块ID 引用次数 引用占比 模块相对路径 模块总名\r\n`
      );
      arrayObj.forEach(ele => {
        let key = ele[0];
        let value = ele[1];
        fs.appendFileSync(
          depStaticsPath,
          `${key} ${value} ${value / 18} ${findMap.get(key)} ####\r\n`
        );
      });
    }
  });
}

// main
fileDisplay(`${process.cwd()}/__async_load_output__`);
